<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="规则名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleName">
              <a-input v-model="model.ruleName" placeholder="请输入规则名称" ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规则类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleType">
              <j-dict-select-tag type="list" v-model="model.ruleType" dictCode="rule_type" placeholder="请选择规则类型" @change="changeRuleType();"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="执行角色" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="roleId" v-if="model.ruleType == 1">
              <j-dict-select-tag type="list" v-model="model.roleId" dictCode="sys_role,role_name,id" placeholder="请选择执行角色" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="执行部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="departId">
              <j-select-depart v-model="model.departId" :multi="true"  />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="执行周期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executeCycle" v-if="model.ruleType == 1">
              <j-dict-select-tag type="list" v-model="model.executeCycle" dictCode="execute_cycle" placeholder="请选择执行周期" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="执行次数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="executeNum" v-if="model.ruleType == 1">
              <a-input-number v-model="model.executeNum" placeholder="请输入执行次数" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="规则状态" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="ruleStatus">
              <j-dict-select-tag type="list" v-model="model.ruleStatus" dictCode="rule_status" placeholder="请选择规则状态" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-textarea v-model="model.remark" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="线路" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="lineId">
              <j-dict-select-tag type="list" v-model="model.lineId" dictCode="ins_line,line_name,id" placeholder="请选择线路" />
            </a-form-model-item>
          </a-col>-->
<!--          <a-col :span="24">
            <a-form-model-item label="人员" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">
              <j-select-user-by-dep v-model="model.userId" :multi="true" />
            </a-form-model-item>
          </a-col>-->


        </a-row>
      </a-form-model>
    </a-spin>
  </j-modal>
</template>

<script>

  import { httpAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: "InsRuleModal",
    components: {
    },
    data () {
      return {
        title:"操作",
        width:800,
        visible: false,
        model:{
            executeCycle:"1",
            executeNum:1,
            ruleType:"1",
            ruleStatus:"1",
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        validatorRules: {
           ruleName: [
              { required: true, message: '请输入规则名称!'},
             { validator: (rule, value, callback) => validateDuplicateValue('ins_rule', 'rule_name', value, this.model.id, callback)},
           ],
          roleId: [
              { required: true, message: '请输入执行角色!' }
          ],
          ruleStatus: [
            { required: true, message: '请选择规则状态!'},
          ],
           ruleType: [
              { required: true, message: '请选择规则类型!'},
           ],
           executeCycle: [
              { required: true, message: '请输入执行周期!'},
           ],
           executeNum: [
              { required: true, message: '请输入执行次数!'},
           ],
        },
        url: {
          add: "/inspect/insRule/add",
          edit: "/inspect/insRule/edit",
        }

      }
    },
    created () {
    //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.$refs.form.clearValidate();
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }else{
             return false
          }
        })
      },
      handleCancel () {
        this.close()
      },
      changeRuleType(value) {
        console.info(this.model.ruleType);
        // this.$nextTick(() => {
        //   this.$refs.form.validateField(['roleId','component']);
        // });
      }

    }
  }
</script>
